package cn.com.w.bootfreemarker.handler;

import freemarker.core.Environment;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.Writer;

/**
 * freemarker自定义异常处理器
 */
@Component
public class CustomTemplateExceptionHandler implements TemplateExceptionHandler {
    @Override
    public void handleTemplateException(TemplateException te,
                                        Environment env,
                                        Writer out) throws TemplateException {
        try {
            out.write("[ERROR: " + te.getMessage() + "]");
        } catch (IOException e) {
            throw new TemplateException("Failed to print error message. Cause: " + e, env);
        }
    }
}
